home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_11
/
phillip2
/
txtrsubs.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-13
|
25KB
|
928 lines
/***********************************************
*
* file d:\cips\txtrsubs.c
*
* Functions: This file contains
* sigma
* skewness
* amean
* adifference
* difference_array
* hurst
* compare
* get_texture_options
*
* Purpose:
* These functions calculate measures
* that help distinguish textures.
*
* External Calls:
* wtiff.c - round_off_image_size
* create_file_if_needed
* write_array_into_tiff_image
* tiff.c - read_tiff_header
* rtiff.c - read_tiff_image
* edge.c - fix_edges
* fitt.c - fit
* filter.c - sort_elements
*
* Modifications:
* 12 August 1993- created
*
*************************************************/
#include "cips.h"
/*******************************************
*
* sigma(..
*
* This calculates the variance and the
* sigma for a sizeXsize area.
*
* It sums the squares of the difference
* between each pixel and the mean of
* the area and divides that by the
* number of pixels in the area.
*
* The output image is set to the square
* root of the variance since the variance
* will almost certainly be out of range
* for the image. The square root of the
* variance will be sigma.
*
*******************************************/
sigma(in_name, out_name, the_image, out_image,
il, ie, ll, le, size, threshold, high)
char in_name[], out_name[];
int il, ie, ll, le,
high, threshold, size;
short the_image[ROWS][COLS],
out_image[ROWS][COLS];
{
int a, b, count, i, j, k,
max, mean, new_hi, new_low,
sd2, sd2p1;
short sigma;
struct tiff_header_struct image_header;
unsigned long diff, variance;
sd2 = size/2;
sd2p1 = sd2 + 1;
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
max = 255;
new_hi = 250;
new_low = 16;
if(image_header.bits_per_pixel == 4){
new_hi = 10;
new_low = 3;
max = 16;
}
/***************************
*
* Loop over image array
*
****************************/
printf("\n");
for(i=sd2; i<ROWS-sd2; i++){
if( (i%10) == 0) printf("%d ", i);
for(j=sd2; j<COLS-sd2; j++){
/*****************************
*
* Run through the small area
* and calculate the mean.
*
******************************/
mean = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
mean = mean + the_image[i+a][j+b];
}
}
mean = mean/(size*size);
/*****************************
*
* Run through the small area
* again and the calculate the
* variance.
*
******************************/
variance = 0;
diff = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
diff = the_image[i+a][j+b] - mean;
variance = variance + (diff*diff);
}
}
variance = variance/(size*size);
sigma = sqrt(variance);
if(sigma > max) sigma = max;
out_image[i][j] = sigma;
} /* ends loop over j */
} /* ends loop over i */
/* if desired, threshold the output image */
if(threshold == 1){
for(i=0; i<ROWS; i++){
for(j=0; j<COLS; j++){
if(out_image[i][j] > high){
out_image[i][j] = new_hi;
}
else{
out_image[i][j] = new_low;
}
}
}
} /* ends if threshold == 1 */
fix_edges(out_image, sd2);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
} /* ends sigma */
/*******************************************
*
* skewness(..
*
* This calculates the skewness for a
* sizeXsize area.
*
* Look at Levine's book page 449 for
* the formula.
* "Vision in Man and Machine" by
* Martin D. Levine, McGraw Hill, 1985.
*
*******************************************/
skewness(in_name, out_name, the_image, out_image,
il, ie, ll, le, size, threshold, high)
char in_name[], out_name[];
int il, ie, ll, le,
high, threshold, size;
short the_image[ROWS][COLS],
out_image[ROWS][COLS];
{
int a, b, count, i, j, k,
max, mean, new_hi, new_low,
sd2, sd2p1;
long cube;
short sigma, skew;
struct tiff_header_struct image_header;
unsigned long diff, sigma3, variance;
sd2 = size/2;
sd2p1 = sd2 + 1;
create_file_if_needed(in_name, out_name, out_image);
read_tiff_image(in_name, the_image, il, ie, ll, le);
max = 255;
new_hi = 250;
new_low = 16;
if(image_header.bits_per_pixel == 4){
new_hi = 10;
new_low = 3;
max = 16;
}
/***************************
*
* Loop over image array
*
****************************/
printf("\n");
for(i=sd2; i<ROWS-sd2; i++){
if( (i%10) == 0) printf("%d ", i);
for(j=sd2; j<COLS-sd2; j++){
/*****************************
*
* Run through the small area
* and calculate the mean.
*
******************************/
mean = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
mean = mean + the_image[i+a][j+b];
}
}
mean = mean/(size*size);
/*****************************
*
* Run through the small area
* again and the calculate the
* variance and the cube.
*
******************************/
variance = 0;
diff = 0;
cube = 0;
for(a=-sd2; a<sd2p1; a++){
for(b=-sd2; b<sd2p1; b++){
diff = the_image[i+a][j+b] - mean;
cube = cube + (diff*diff*diff);
variance = variance + (diff*diff);
}
}
variance = variance/(size*size);
sigma = sqrt(variance);
sigma3 = sigma*sigma*sigma;
if(sigma3 == 0)
sigma3 = 1;
skew = cube/(sigma3*size*size);
out_image[i][j] = skew;
if(out_image[i][j] > max)
out_image[i][j] = max;
} /* ends loop over j */
} /* ends loop over i */
/* if desired, threshold the output image */
if(threshold == 1){
for(i=0; i<ROWS; i++){
for(j=0; j<COLS; j++){
if(out_image[i][j] > high){
out_image[i][j] = new_hi;
}
else{
out_image[i][j] = new_low;
}
}
}
} /* ends if threshold == 1 */
fix_edges(out_image, sd2);
write_array_into_tiff_image(out_name, out_image,
il, ie, ll, le);
} /* ends skewness */
/*******************************************
*
* amean(..
*
* This calculates the mean measure
* for a sizeXsize area.
*
* Look at Levine's book page 451 for
* the formula.
* "Vision in Man and Machine" by
* Martin D. Levine, McGraw Hill, 1985.
*
******************************